package jersey;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.representation.Form;

import superkigav.shared.monitor.IMonitorRequest;


public class MonitorRequestClient implements IMonitorRequest {
    public static void main(String[] args) {

    }
    
    private static URI getBaseURI() {
            return UriBuilder.fromUri(
                            "http://localhost:8080/DispatcherMonitor/").build();
    }

    public boolean addServer(String host, int port) {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource service = client.resource(getBaseURI());
        Form form = new Form();
        form.add("host", host);
        form.add("port", port);
        ClientResponse response = service.path("monitor").path("addServer").type(
                MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form);
        if(response.getStatus()==200)
            return response.getEntity(boolean.class);
        else
            return false;
        
    }

    public void iAmAlive() {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource service = client.resource(getBaseURI());
        Form form = new Form();
        ClientResponse response = service.path("monitor").path("iAmAlive").type(
                MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form);
        return;
    }

}
